package cn.tworice.client.web;

import cn.tworice.client.service.InfoService;
import cn.tworice.client.service.SacnService;
import cn.tworice.common.entity.code.*;
import cn.tworice.common.inducts.InductsListener;
import cn.tworice.common.vo.*;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/client/sacn")
@CrossOrigin
public class SacnClient {
    @Autowired
    private SacnService service;

    @Autowired
    private InfoService infoService;

    @GetMapping("info")
    public RequestResult info(long id) {
        RequestResult result = new RequestResult();
        Sacn info = service.getInfo(id);
        result.appendData("info",info);
        return result;
    }

    @GetMapping("")
    public RequestResult sacn(String addr){
        RequestResult result = new RequestResult();
        List<Port> list=service.sacn(addr);
        result.appendData("port",list);
        return result;
    }

    @GetMapping("port")
    public RequestResult port(String addr,Integer port){
        RequestResult result = new RequestResult();
        Port port1=service.sacn(addr, port);
        result.appendData("info",port1);
        return result;
    }

    @GetMapping("list")
    public RequestResult list(Integer page, Integer pageSize, Sacn entity) {
        RequestResult result = new RequestResult();
        List<Sacn> list = service.getList(page, pageSize, entity);
        int total = service.getTotal(entity);
        result.appendData("list", list);
        result.appendData("total", total);
        return result;
    }

    @PostMapping("add")
    public RequestResult add(Sacn entity) {
        RequestResult result = new RequestResult();
        service.set(entity);
        return result;
    }

    @DeleteMapping("del")
    public RequestResult del(Integer[] ids) {
        RequestResult result = new RequestResult();
        service.del(ids);
        return result;
    }

    @PostMapping("inducts")
    public RequestResult inducts(MultipartFile file) {
        RequestResult result = new RequestResult();
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, Sacn.class, new InductsListener(service)).sheet().doRead();
            result.setStatus(new StatusCode(200, "批量导入数据成功")
            );
        } catch (Exception ex) {
            ex.printStackTrace();
            result.setStatus(new StatusCode(204, "批量导入数据失败")
            );
        }
        return result;
    }

    /**
     * 保存此次扫描记录
     * @param sacn
     * @param info
     * @return
     */
    @PostMapping("save")
    public RequestResult save(Sacn sacn,String info){
        RequestResult result = new RequestResult();
        List<Info> list = JSON.parseArray(info, Info.class);
        service.saveInfo(sacn,list);
        return result;
    }
}